﻿/*+===================================================================
File:      FBullet.cpp

Summary:   Định nghĩa các phương thức của CFBullet.
===================================================================+*/

#include "FBullet.h"
#include "Gun.h"

#define R 30  // Bán kính xoay của đạn.

CFBullet::CFBullet(D3DXVECTOR2 pos, int id, int typeID, float angle, int charDirectionX) : CBullet(pos, id, typeID, angle)
{
	m_nCharDirectionX = charDirectionX;
	m_pSprite = m_pResourceManager->GetSprite(ID_SPRITE_F_BULLET);
	m_v.x = 400 * cos(angle * PI / 180.0f);
	m_v.y = 400 * sin(angle * PI / 180.0f);

	m_centerPos.x = pos.x + cos(angle * PI / 180.0f) * R;
	m_centerPos.y = pos.y + sin(angle * PI / 180.0f) * R;

	m_fRotateAngle = angle - 180;
}

CFBullet::~CFBullet()
{
}

void CFBullet::Update(float timeFrame)
{
	if (!m_bIsLive)
		return;

	D3DXVECTOR2 posInViewPort = CViewport::GetInstance()->TransformMatrix(m_pos);

	if (posInViewPort.x < 0 || posInViewPort.x > SCREEN_WIDTH || posInViewPort.y < 0 || posInViewPort.y > SCREEN_HEIGHT)
	{
		m_bIsLive = false;
		m_pContainerGun->ReloadBullet();
		return;
	}

	m_pos.x = R * (cos(m_fRotateAngle * PI / 180.0f)) + m_centerPos.x;
	m_pos.y = R * (sin(m_fRotateAngle * PI / 180.0f)) + m_centerPos.y;
	m_fRotateAngle -= 11 * 180 * timeFrame * m_nCharDirectionX;

	//// Tịnh tiến tâm xoay.
	m_centerPos.x += m_v.x * timeFrame;
	m_centerPos.y += m_v.y * timeFrame;

	m_pSprite->Update(timeFrame);
	UpdateBox(timeFrame);

	m_nCount = m_nCount + 1;
}